/*
 * @# FaxTestServlet.java 2011-11-22 下午06:04:26
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.experiments.ws.fax.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.util.ResourceUtils;

import cn.eshore.bnet.sag.fax.DeliveryState;
import cn.eshore.bnet.sag.fax.PolicyException_Exception;
import cn.eshore.bnet.sag.fax.ServiceException_Exception;

import com.ianswer.common.fax.FaxDispatcher;
import com.ianswer.common.fax.ct.CtFaxQuery;

/**
 * 传真测试servlet
 * 
 * @author tian qingsen
 */
public class FaxTestServlet extends HttpServlet {

	private static final long serialVersionUID = 8108389975922619225L;
	
	private static Logger log = Logger.getLogger(FaxTestServlet.class);

	@Override
	public void init() throws ServletException {
		super.init();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String testContent = req.getParameter("testContent");
		if ("getState".equalsIgnoreCase(testContent)) {
			getFaxState(req, resp);
		} else {
			sendFax(req, resp);
		}
	}
	
	private void sendFax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String receiver = req.getParameter("receiver");
		String subject = req.getParameter("subject");
		String notifyUrl = req.getParameter("notifyUrl");
		String[] attachments = req.getParameterValues("attachments");
		
		List<File> attachmentFiles = new ArrayList<File>();
		if (ArrayUtils.isNotEmpty(attachments)) {
			for (String attachment : attachments) {
				attachmentFile(attachment, attachmentFiles);
			}
		}
		if (StringUtils.isNotBlank(receiver) && StringUtils.isNotBlank(subject)) {
			FaxDispatcher dispatcher = new FaxDispatcher();
			if (StringUtils.isNotBlank(notifyUrl)) {
				dispatcher.sendAndNotify(receiver, subject, attachmentFiles, notifyUrl);
			} else {
				dispatcher.send(receiver, subject, attachmentFiles);
			}
		}

		req.setAttribute("receiver", receiver);
		req.setAttribute("subject", subject);
		req.getRequestDispatcher("/faxTest.jsp").forward(req, resp);
	}

	private void attachmentFile(String attachment, List<File> attachmentFiles) throws FileNotFoundException {
		if (attachment.equals("txt")) {
			log.info("send txt ...");
			attachmentFiles.add(ResourceUtils.getFile("classpath:/fax.txt"));
		} else if (attachment.equals("gif")) {
			log.info("send gif ...");
			attachmentFiles.add(ResourceUtils.getFile("classpath:/fax.gif"));
		} else if (attachment.equals("jpg")) {
			log.info("send jpg ...");
			attachmentFiles.add(ResourceUtils.getFile("classpath:/fax.jpg"));
		} else if (attachment.equals("html")) {
			log.info("send html ...");
			attachmentFiles.add(ResourceUtils.getFile("classpath:/fax.html"));
		} else if (attachment.equals("pdf")) {
			log.info("send pdf ...");
			attachmentFiles.add(ResourceUtils.getFile("classpath:/fax.pdf"));
		}
	}
	
	private void getFaxState(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String requestId = req.getParameter("requestId");
		
		List<DeliveryState> stateList = new ArrayList<DeliveryState>();
		try {
			CtFaxQuery cfq = new CtFaxQuery();
			stateList = cfq.getState(requestId);
		} catch (ServiceException_Exception e) {
			log.error("getFaxState fail, error detail: [" + e.getErrorCode() + "] " + e.getErrorMessage(), e);
		} catch (PolicyException_Exception e) {
			log.error("getFaxState fail, error detail: [" + e.getErrorCode() + "] " + e.getErrorMessage(), e);
		} catch (ParseException e) {
			log.error("getFaxState fail, error detail: [" + e.getMessage() + "]", e);
		}
		
		String stateStr = "";
		for (DeliveryState state : stateList) {
			stateStr = ("[receive number: " + state.getReceiver() + ", state: " + state.getState() + "]");
		}
		
		req.setAttribute("requestId", requestId);
		req.setAttribute("stateStr", stateStr);
		req.getRequestDispatcher("/faxStateQuery.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

	@Override
	public void destroy() {
		super.destroy();
	}

}
